﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		ShortContentDal 
 * 
 * Created:		2013-9-3 21:06:14
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Besture.Common;
using Besture.Model;

namespace Besture.Dal
{
    public class ShortContentDal : BaseDataAccess
    {
        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public ShortContentModel GetContent(string entryKey)
        {
            try
            {
                string sql = string.Format(@"SELECT * FROM TBShortContent WHERE EntryKey = '{0}'", entryKey);
                DbCommand ldcDBCommand = base.GetCommand(sql);
                DataTable table = base.GetDataSet(ldcDBCommand).Tables[0];
                if (table.Rows.Count > 0)
                    return ModelConvertHelper<ShortContentModel>.ConvertToModelList(table)[0];
                else return null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ShortContentModel model)
        {
            var identityId = -1;
            try
            {
                StringBuilder insertSql = new StringBuilder();
                insertSql.Append("INSERT INTO TBShortContent(");
                insertSql.Append("EntryKey,");
                insertSql.Append("EntryTitle,");
                insertSql.Append("EntryValue,");
                insertSql.Append("ExtColumn1,");
                insertSql.Append("ExtColumn2,");
                insertSql.Append("ExtColumn3,");
                insertSql.Append("ExtColumn4,");
                insertSql.Append("ExtColumn5,");
                insertSql.Append("ExtColumn6,");
                insertSql.Append("ExtColumn7,");
                insertSql.Append("ExtColumn8,");
                insertSql.Append("ExtColumn9,");
                insertSql.Append("ExtColumn10,");
                insertSql.Append("CreateBy,");
                insertSql.Append("CreateTime,");
                insertSql.Append("UpdateBy,");
                insertSql.Append("UpdateTime");

                insertSql.Append(") ");
                insertSql.Append(" VALUES(");
                insertSql.Append("@EntryKey,");
                insertSql.Append("@EntryTitle,");
                insertSql.Append("@EntryValue,");
                insertSql.Append("@ExtColumn1,");
                insertSql.Append("@ExtColumn2,");
                insertSql.Append("@ExtColumn3,");
                insertSql.Append("@ExtColumn4,");
                insertSql.Append("@ExtColumn5,");
                insertSql.Append("@ExtColumn6,");
                insertSql.Append("@ExtColumn7,");
                insertSql.Append("@ExtColumn8,");
                insertSql.Append("@ExtColumn9,");
                insertSql.Append("@ExtColumn10,");
                insertSql.Append("@CreateBy,");
                insertSql.Append("@CreateTime,");
                insertSql.Append("@UpdateBy,");
                insertSql.Append("@UpdateTime");

                insertSql.Append(")");
                insertSql.Append(" SELECT @@IDENTITY");
                DbCommand ldcDBCommand = base.GetCommand(insertSql.ToString());
                base.SetParameter(ldcDBCommand, "@EntryKey", DbType.String, model.EntryKey);
                base.SetParameter(ldcDBCommand, "@EntryTitle", DbType.String, model.EntryTitle);
                base.SetParameter(ldcDBCommand, "@EntryValue", DbType.String, model.EntryValue);
                base.SetParameter(ldcDBCommand, "@ExtColumn1", DbType.String, model.ExtColumn1);
                base.SetParameter(ldcDBCommand, "@ExtColumn2", DbType.String, model.ExtColumn2);
                base.SetParameter(ldcDBCommand, "@ExtColumn3", DbType.String, model.ExtColumn3);
                base.SetParameter(ldcDBCommand, "@ExtColumn4", DbType.String, model.ExtColumn4);
                base.SetParameter(ldcDBCommand, "@ExtColumn5", DbType.String, model.ExtColumn5);
                base.SetParameter(ldcDBCommand, "@ExtColumn6", DbType.Int32, model.ExtColumn6);
                base.SetParameter(ldcDBCommand, "@ExtColumn7", DbType.Int32, model.ExtColumn7);
                base.SetParameter(ldcDBCommand, "@ExtColumn8", DbType.Int32, model.ExtColumn8);
                base.SetParameter(ldcDBCommand, "@ExtColumn9", DbType.Int32, model.ExtColumn9);
                base.SetParameter(ldcDBCommand, "@ExtColumn10", DbType.Int32, model.ExtColumn10);
                base.SetParameter(ldcDBCommand, "@CreateBy", DbType.Int32, model.CreateBy);
                base.SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, DateTime.Now);
                base.SetParameter(ldcDBCommand, "@UpdateBy", DbType.Int32, model.UpdateBy);
                base.SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, DateTime.Now);

                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return identityId;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ShortContentModel model)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("UPDATE TBShortContent");
                updateSql.Append(" SET");
                updateSql.Append(" EntryKey = @EntryKey,");
                updateSql.Append(" EntryTitle = @EntryTitle,");
                updateSql.Append(" EntryValue = @EntryValue,");
                updateSql.Append(" ExtColumn1 = @ExtColumn1,");
                updateSql.Append(" ExtColumn2 = @ExtColumn2,");
                updateSql.Append(" ExtColumn3 = @ExtColumn3,");
                updateSql.Append(" ExtColumn4 = @ExtColumn4,");
                updateSql.Append(" ExtColumn5 = @ExtColumn5,");
                updateSql.Append(" ExtColumn6 = @ExtColumn6,");
                updateSql.Append(" ExtColumn7 = @ExtColumn7,");
                updateSql.Append(" ExtColumn8 = @ExtColumn8,");
                updateSql.Append(" ExtColumn9 = @ExtColumn9,");
                updateSql.Append(" ExtColumn10 = @ExtColumn10,");
                updateSql.Append(" CreateBy = @CreateBy,");
                updateSql.Append(" CreateTime = @CreateTime,");
                updateSql.Append(" UpdateBy = @UpdateBy,");
                updateSql.Append(" UpdateTime = @UpdateTime ");

                updateSql.Append(" WHERE ");
                updateSql.Append(" ID = @ID ");

                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, model.ID);
                base.SetParameter(ldcDBCommand, "@EntryKey", DbType.String, model.EntryKey);
                base.SetParameter(ldcDBCommand, "@EntryTitle", DbType.String, model.EntryTitle);
                base.SetParameter(ldcDBCommand, "@EntryValue", DbType.String, model.EntryValue);
                base.SetParameter(ldcDBCommand, "@ExtColumn1", DbType.String, model.ExtColumn1);
                base.SetParameter(ldcDBCommand, "@ExtColumn2", DbType.String, model.ExtColumn2);
                base.SetParameter(ldcDBCommand, "@ExtColumn3", DbType.String, model.ExtColumn3);
                base.SetParameter(ldcDBCommand, "@ExtColumn4", DbType.String, model.ExtColumn4);
                base.SetParameter(ldcDBCommand, "@ExtColumn5", DbType.String, model.ExtColumn5);
                base.SetParameter(ldcDBCommand, "@ExtColumn6", DbType.Int32, model.ExtColumn6);
                base.SetParameter(ldcDBCommand, "@ExtColumn7", DbType.Int32, model.ExtColumn7);
                base.SetParameter(ldcDBCommand, "@ExtColumn8", DbType.Int32, model.ExtColumn8);
                base.SetParameter(ldcDBCommand, "@ExtColumn9", DbType.Int32, model.ExtColumn9);
                base.SetParameter(ldcDBCommand, "@ExtColumn10", DbType.Int32, model.ExtColumn10);
                base.SetParameter(ldcDBCommand, "@CreateBy", DbType.Int32, model.CreateBy);
                base.SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, DateTime.Now);
                base.SetParameter(ldcDBCommand, "@UpdateBy", DbType.Int32, model.UpdateBy);
                base.SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, DateTime.Now);

                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }
    }
}
